// 歌手信息接口
export interface Artist {
  id: number;
  name: string;
  picUrl?: string;
}

// 专辑基础信息接口
interface AlbumBase {
  id: number;
  name: string;
  picUrl: string;
}

// 歌曲信息
export class Song {
  id: number = 0;
  name: string = '';
  ar: Artist[] = [];
  al: AlbumBase = { id: 0, name: '', picUrl: '' };
  dt: number = 0; // 歌曲时长(毫秒)
}

// 歌词行信息（添加了export导出）
export interface LyricLine {
  time: number;    // 时间戳(秒)
  text: string;    // 歌词文本
  active?: boolean; // 是否激活
  progress?: number; // 卡拉OK进度 [0-1]
}

// 增强版歌词类
export class Lyric {
  original: string = '';
  lines: LyricLine[] = [];
  currentLineIndex: number = -1;
  private _offset: number = 0;

  constructor(rawLyric?: string) {
    if (rawLyric) {
      this.load(rawLyric);
    }
  }

  load(rawLyric: string): void {
    this.original = rawLyric;
    this.lines = [];
    this.parseLyric(rawLyric);
    this.sortLines();
  }

  set offset(seconds: number) {
    this._offset = seconds;
    this.lines.forEach(line => line.time += seconds);
    this.sortLines();
  }

  get offset(): number {
    return this._offset;
  }

  private parseLyric(rawLyric: string): void {
    const lineRegex = /^\[(\d+):(\d+)\.(\d+)\](.*)/;
    const lines = rawLyric.split('\n');

    lines.forEach(line => {
      const match = line.match(lineRegex);
      if (match) {
        const minutes = parseInt(match[1]);
        const seconds = parseInt(match[2]);
        const ms = parseInt(match[3].padEnd(3, '0').slice(0, 3));
        const time = minutes * 60 + seconds + ms / 1000;
        const text = match[4].trim();

        if (text) {
          this.lines.push({
            time: time + this._offset,
            text,
            active: false,
            progress: 0
          });
        }
      }
    });
  }

  private sortLines(): void {
    this.lines.sort((a, b) => a.time - b.time);
  }

  update(currentTime: number): void {
    if (!this.lines.length) return;

    this.lines.forEach(line => {
      line.active = false;
      line.progress = 0;
    });

    let activeLine = -1;
    for (let i = this.lines.length - 1; i >= 0; i--) {
      if (currentTime >= this.lines[i].time) {
        activeLine = i;
        break;
      }
    }

    if (activeLine >= 0) {
      this.currentLineIndex = activeLine;
      this.lines[activeLine].active = true;

      if (activeLine < this.lines.length - 1) {
        const duration = this.lines[activeLine + 1].time - this.lines[activeLine].time;
        const elapsed = currentTime - this.lines[activeLine].time;
        this.lines[activeLine].progress = Math.min(elapsed / duration, 1);
      } else {
        this.lines[activeLine].progress = 1;
      }
    }
  }

  getCurrentLine(): LyricLine | null {
    return this.currentLineIndex >= 0 ? this.lines[this.currentLineIndex] : null;
  }

  getLinesAround(count: number = 5): LyricLine[] {
    if (this.currentLineIndex < 0) return [];
    const start = Math.max(0, this.currentLineIndex - Math.floor(count / 2));
    const end = Math.min(this.lines.length, start + count);
    return this.lines.slice(start, end);
  }
}

// 专辑完整信息
export class Album {
  id: number = 0;
  name: string = '';
  picUrl: string = '';
  artist: Artist = { id: 0, name: '' };
  description: string = '';
  publishTime: number = 0;
  songs: Song[] = [];
}